package com.qdlc.p2p.dal.dao.impl;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.UserBaseInfoDao;
import com.qdlc.p2p.dal.dto.UserBaseInfo;

/**
 * 用户基本信息
 * 
 * @author wzh
 * @version 2.0
 * @since 2014年11月4日
 */
@Repository("userBaseInfoDao")
public class UserBaseInfoDaoImpl extends BaseDaoImpl<UserBaseInfo> implements UserBaseInfoDao {

	@Override
	public UserBaseInfo findByUserId(long userId) {
		return this.findObjByProperty("user.userId", userId);
	}

	@Override
	public int getEducationsCountByDateAndEducation(String date, int education) {
		String sql = "select count(distinct u.user_id) from " + Global.DB_PREFIX + "user_base_info u, " + Global.DB_PREFIX + "borrow_tender b where u.user_id=b.user_id and date_format(b.add_time,'%Y-%m')=? and education=?";
		Query query = em.createNativeQuery(sql).setParameter(1, date).setParameter(2, education);
		return Integer.parseInt(query.getSingleResult() + "");
	}
	@Override
	public int getMaritalStatusCountByDateAndMaritalStatus(String date, int maritalStatus) {
		String sql = "select count(distinct u.user_id) from " + Global.DB_PREFIX + "user_base_info u, " + Global.DB_PREFIX + "borrow_tender b where u.user_id=b.user_id and date_format(b.add_time,'%Y-%m')=? and marital_status=?";
		Query query = em.createNativeQuery(sql).setParameter(1, date).setParameter(2, maritalStatus);
		return Integer.parseInt(query.getSingleResult() + "");
	}
	@Override
	public int getBorrowEducationsCountByDateAndEducation(String date, int education) {
		String sql = "select count(distinct u.user_id) from " + Global.DB_PREFIX + "user_base_info u, " + Global.DB_PREFIX + "borrow b where u.user_id=b.user_id and date_format(b.add_time,'%Y-%m')=? and education=?";
		Query query = em.createNativeQuery(sql).setParameter(1, date).setParameter(2, education);
		return Integer.parseInt(query.getSingleResult() + "");
	}
	@Override
	public int getBorrowMaritalStatusCountByDateAndMaritalStatus(String date, int maritalStatus) {
		String sql = "select count(distinct u.user_id) from " + Global.DB_PREFIX + "user_base_info u, " + Global.DB_PREFIX + "borrow b where u.user_id=b.user_id and date_format(b.add_time,'%Y-%m')=? and marital_status=?";
		Query query = em.createNativeQuery(sql).setParameter(1, date).setParameter(2, maritalStatus);
		return Integer.parseInt(query.getSingleResult() + "");
	}

}
